package com.example.cardgame

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class SecondActivity : AppCompatActivity() {
    private lateinit var tvStudentId: TextView
    private lateinit var tvName: TextView
    private lateinit var tvGender: TextView
    private lateinit var tvPhone: TextView
    private lateinit var tvEmail: TextView
    private lateinit var tvHighestScore: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)

        // 处理系统栏边距
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 初始化展示控件
        tvStudentId = findViewById(R.id.tv_student_id)
        tvName = findViewById(R.id.tv_name)
        tvGender = findViewById(R.id.tv_gender)
        tvPhone = findViewById(R.id.tv_phone)
        tvEmail = findViewById(R.id.tv_email)
        tvHighestScore = findViewById(R.id.tv_highest_score)

        // 接收MainActivity传递的数据
        val intent = intent
        val studentId = intent.getStringExtra("studentId") ?: ""
        val name = intent.getStringExtra("name") ?: ""
        val gender = intent.getStringExtra("gender") ?: ""
        val phone = intent.getStringExtra("phone") ?: ""
        val email = intent.getStringExtra("email") ?: ""
        val highestScore = intent.getStringExtra("highestScore") ?: "0"

        // 展示数据
        tvStudentId.text = "学号：$studentId"
        tvName.text = "姓名：$name"
        tvGender.text = "性别：$gender"
        tvPhone.text = "电话：$phone"
        tvEmail.text = "邮箱：$email"
        tvHighestScore.text = "最高分数：$highestScore"

        // 修改个人信息按钮（返回填写页）
        findViewById<Button>(R.id.btn_modify_second).setOnClickListener {
            startActivity(Intent(this, MainActivity::class.java))
        }

        // 开始纸牌游戏按钮（跳转至ThirdActivity）
        findViewById<Button>(R.id.btn_start_game).setOnClickListener {
            val gameIntent = Intent(this, ThirdActivity::class.java)
            // 启动Activity并等待返回结果（请求码100）
            startActivityForResult(gameIntent, 100)
        }
    }

    // 接收ThirdActivity返回的分数结果
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == RESULT_OK) {
            // 获取游戏返回的分数
            val currentScore = data?.getIntExtra("currentScore", 0) ?: 0
            // 获取当前显示的最高分数
            val currentHighest = tvHighestScore.text.toString()
                .replace("最高分数：", "")
                .toIntOrNull() ?: 0
            // 更新最高分数（如果当前分数更高）
            if (currentScore > currentHighest) {
                tvHighestScore.text = "最高分数：$currentScore"
                // 此处可添加持久化存储逻辑（如SharedPreferences）
            }
        }
    }
}